import axios from 'axios';
import { Dialog, Toast } from 'vant';

// create an axios instance
const service = axios.create({
  baseURL: 'http://39.97.162.130:8080', // api 的 base_url
  timeout: 5000, // request timeout
  // headers: {'Conten-Type': 'multipart/form-data'}
})
// Vue.config.productionTip = false
// Vue.prototype.service = service

// request interceptor
service.interceptors.request.use(
    config => {
    if (!config.headers['X-Tdwmall-Token']) { 
      config.headers['X-Tdwmall-Token'] = `${window.localStorage.getItem(
        'Authorization'
      ) || ''}`;
    }
    config.headers['Conten-Type']  = 'multipart/form-data'
    return config;
  },
  err => Promise.reject(err)
)

// response interceptor
service.interceptors.response.use(
  response => {
    const res = response.data

    if (res.errno === 501) {
        Toast.fail('请登录');
        setTimeout(() => {
          window.location = '#/login/'
        }, 1500)
      return Promise.reject('error')
    } if (res.errno === 401) {
      Toast.fail('参数不对');
      return Promise.reject('error')
    } if (res.errno === 402) {
      Toast.fail('参数值不对');
      return Promise.reject('error')
    } else {
      return response
    }
  }, error => {
    console.log('err' + error)// for debug
    Dialog.alert({
        title: '警告',
        message: '登录连接超时'
      });
    return Promise.reject(error)
  })

export default service
